package com.hy.ai.controller;

import com.hy.ai.config.MySqlChatMemory;
import com.hy.ai.entity.vo.MessageVo;
import com.hy.ai.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ai/history/")
public class ChatHistoryController {
    private final ChatHistoryRepository repository;

    //查询会话历史
    private final MySqlChatMemory chatMemory;
    @GetMapping("/{type}")
    public List<String> query(@PathVariable("type") String type){
        return repository.query(type,"1");
    }
    //根据id查询记录
    @GetMapping("/{type}/{chatId}")
    public List<MessageVo> getChatHistory(@PathVariable("type") String type
            ,@PathVariable("chatId") String chatId){
        List<Message> messages = chatMemory.get(chatId, Integer.MAX_VALUE);
        if(messages==null){
            return List.of();
        }
        return messages.stream().map(MessageVo::new).toList();
    }
}
